#line 1 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
#line 1 "c:/users/toanrv/dropbox/pic project/source_and_design/smarthome-centreal controller/source-svn/ds1307.h"
#line 24 "c:/users/toanrv/dropbox/pic project/source_and_design/smarthome-centreal controller/source-svn/ds1307.h"
typedef struct{
 unsigned short sec;
 unsigned short min;
 unsigned short week_day;
 unsigned short day;
 unsigned short hour;
 unsigned short month;
 unsigned short year;
} RTC_Time;

void DS1307_Init();
unsigned short DS1307_BCD2Dec(unsigned char n);
unsigned short DS1307_Dec2BCD(unsigned char n) ;

void DS1307_GetDateTime(RTC_Time *p) ;

void DS1307_LCD_Display_Time(RTC_Time *p);

void DS1307_SetDateTime(RTC_Time *p);
#line 1 "c:/users/toanrv/dropbox/pic project/source_and_design/smarthome-centreal controller/source-svn/rfremotedecode.h"
#line 24 "c:/users/toanrv/dropbox/pic project/source_and_design/smarthome-centreal controller/source-svn/rfremotedecode.h"
 typedef enum RFRemoteDecode_State { RFRemoteDecode_Detect_Sync_Bit,
 RFRemoteDecode_Detect_Min_High_Pulse,
 RFRemoteDecode_Detect_AddrnDat,
 RFRemoteDecode_Process_Button,
 RFRemoteDecode_Establish_Code,
 RFRemoteDecode_Control_Actuators,
 } _RFRemoteDecode_State;

 typedef enum Learningmode { SingleONOFF,
 AllON,
 AllOFF,
 Clearance,
 }_Learningmode;
#line 66 "c:/users/toanrv/dropbox/pic project/source_and_design/smarthome-centreal controller/source-svn/rfremotedecode.h"
 unsigned long RFRemoteDecode_GetAddr ( unsigned short * RFRemoteBuff);
 unsigned int RFRemoteDecode_GetData ( unsigned short * RFRemoteBuff);
#line 648 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 unsigned short Tmr1_Cnt ;
 unsigned int Tmr0_Cnt ;
 unsigned short Tmr0_Cnt1;
 unsigned int HighTickCounter ;
 unsigned int TempHTC[10];
 unsigned int TempFiveLow;
 unsigned int TempOneLow;
 unsigned short TempCheck;
 unsigned int TempHTC1;
 unsigned int LowTickCounter ;
 unsigned short CntFrame ;
 _RFRemoteDecode_State State ;
 unsigned short SyncBitCouter;

 bit Detect31aPulseFlag ;
 bit Detect1aPulseFlag ;
 bit DetectSyncBit ;
 bit EndOfTimingWindows ;
 bit SyncBitCouter1 ;

 bit LearnCodeEvent ;
 bit j, k ;

 unsigned short RFReceiverBuffer[12];
 unsigned short RFReceiverBufferCounter ;

 unsigned int TIME_1a_LOW;
 unsigned int TIME_1a_HIGH;
 unsigned int TIME_3a_LOW;
 unsigned int TIME_3a_HIGH;
 unsigned int TIME_31a_LOW;
 unsigned int TIME_31a_HIGH ;
 unsigned short i;
 unsigned int HTC[3] ;

 unsigned long RFRemoteAddrCode;
 unsigned int RFRemoteDatCode ;

 unsigned short EEPROM_Codes_Cnt;
 unsigned short Cnt;


 unsigned short CodeIndex[5];


 _Learningmode LearnMode;


 char txt1[8];
#line 703 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 void interrupt(void){

 if(TMR0IF_bit){



 if (State == RFRemoteDecode_Detect_Min_High_Pulse){
 Tmr0_Cnt++;
 Tmr0_Cnt1++;

 if(Tmr0_Cnt >=  5 ){

 RC5_bit = ~ RC5_bit;
 Tmr0_Cnt = 0;
 EndOfTimingWindows = 1;
 }
 if(Tmr0_Cnt1 >=  122 ){

 State = RFRemoteDecode_Detect_Sync_Bit ;
 Tmr0_cnt1 = 0;
 LearnCodeEvent = 0;
 }


 TMR0 = 0x00;
 }
 else if( State == RFRemoteDecode_Process_Button ){

 Tmr0_cnt++;


 switch (Tmr0_cnt){
 case  61  :
 RC4_bit = 1;
 LearnMode = SingleONOFF;
 break;
 case  122  :
 RC4_bit = 0;
 LearnMode = AllON ;
 break;
 case  183  :
 RC4_bit = 1;
 LearnMode = AllOFF;
 break;
 case  244  :
 RC4_bit = 0;
 LearnMode = Clearance;
 break;
 }
 }
 else{
 Tmr0_Cnt++;
 if( Tmr0_Cnt == 6){
 Tmr0_Cnt = 0;
 j=0;
 }
 }

 TMR0IF_bit = 0;
 }
 if( PIR1.TMR1IF){

 Tmr1_Cnt++;

 TMR1H = 0x00;
 TMR1L = 0x00;
 PIR1.TMR1IF = 0;
 }
 if(INTF_bit){
 if(State == RFRemoteDecode_Detect_Min_High_Pulse ||
 (State == RFRemoteDecode_Detect_Sync_Bit) ){


 if(RB0_bit == 0){
#line 781 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 HighTickCounter = (unsigned int )(TMR1L | (TMR1H << 8));
 TMR1L = 0;
 TMR1H = 0;
 if( (HighTickCounter > 0 )&&(HighTickCounter < 2048) &&(Tmr1_Cnt==0) ){
#line 789 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 Detect1aPulseFlag =1 ;
 }
 else{
 Detect1aPulseFlag = 0;
 Tmr1_Cnt =0;

 }
 INTEDG_bit = 1;
 }

 else {
#line 805 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 LowTickCounter = (TMR1L | (TMR1H << 8)) ;
 TMR1L = 0;
 TMR1H = 0;

 TempFiveLow = HighTickCounter <<5;
 TempOneLow = HighTickCounter <<3;
 if((LowTickCounter >= ((TempFiveLow)-(TempOneLow))) &&
 (LowTickCounter <= (TempFiveLow)+ (TempOneLow)) &&
 (Detect1aPulseFlag) && (Tmr1_Cnt ==0) && (LowTickCounter!=0)){
#line 819 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 if(State == RFRemoteDecode_Detect_Min_High_Pulse){
 DetectSyncBit = 1;

 if(SyncBitCouter <=2){
 *(HTC + SyncBitCouter) = HighTickCounter;
 }
 SyncBitCouter++;


 }
 if(State == RFRemoteDecode_Detect_Sync_Bit){
 SyncBitCouter1 = 1;


 }
 }
 else {
 LowTickCounter = 0;
 HighTickCounter = 0;

 }

 Detect1aPulseFlag = 0;
 Tmr1_Cnt = 0;
 INTEDG_bit = 0;

 }

 }
#line 852 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 if(State == RFRemoteDecode_Detect_AddrnDat)
 {


 if(RB0_bit == 0){
 HighTickCounter = (unsigned int )(TMR1L | (TMR1H << 8));
 TMR1L = 0;
 TMR1H = 0;
 INTEDG_bit = 1;

 }

 else {

 LowTickCounter = (TMR1L | (TMR1H << 8)) ;
 TMR1L = 0;
 TMR1H = 0;
 if (( TIME_1a_LOW <= HighTickCounter ) &&
 ( HighTickCounter <= TIME_1a_HIGH ) &&
 ( TIME_3a_LOW <= LowTickCounter ) &&
 ( LowTickCounter <= TIME_3a_HIGH ) ){




 RFReceiverBuffer[RFReceiverBufferCounter >> 3] |= 0b0001 << (RFReceiverBufferCounter & 0x07);
 RFReceiverBufferCounter += 4;
 HighTickCounter = 0;
 LowTickCounter = 0;
 }
 else if((TIME_3a_LOW <= HighTickCounter ) &&
 ( HighTickCounter <= TIME_3a_HIGH ) &&
 ( TIME_1a_LOW <= LowTickCounter ) &&
 ( LowTickCounter <= TIME_1a_HIGH) ) {


 RFReceiverBuffer[RFReceiverBufferCounter >> 3] |= 0b0111 << (RFReceiverBufferCounter & 0x07);
 RFReceiverBufferCounter += 4;
 HighTickCounter = 0;
 LowTickCounter = 0;
 }
 else{

 HighTickCounter = 0;
 LowTickCounter = 0;
 RFReceiverBufferCounter = 0;
 Tmr1_Cnt = 0;
 State = RFRemoteDecode_Detect_Sync_Bit ;
 TMR0IE_bit = 1;



 }
 RFReceiverBuffer[(RFReceiverBufferCounter >> 3) + 1] = 0x00 ;
 INTEDG_bit = 0;
 }
 if(RFReceiverBufferCounter >= 96){
 CntFrame = 1;

 }



 }
 INTF_bit = 0;
 }
 }
#line 922 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 void main(void){

 UART1_Init(9600);
 Delay_ms(100);
 UART1_Write_Text("Start");

 Tmr1_Cnt = 0;
 Tmr0_Cnt = 0;
 Tmr0_Cnt1 = 0;
 HighTickCounter = 0;
 LowTickCounter = 0;
 CntFrame = 0;
 SyncBitCouter = 0;

 TempFiveLow = 0;
 TempOneLow = 0;

 Detect31aPulseFlag = 0 ;
 Detect1aPulseFlag = 0 ;
 DetectSyncBit = 0 ;
 EndOfTimingWindows = 0 ;
 SyncBitCouter1 = 0 ;
 j = 0 ;
 k = 0 ;

 TIME_1a_HIGH = 0;
 TIME_1a_LOW = 0;
 TIME_3a_HIGH = 0;
 TIME_3a_LOW = 0;
 TIME_31a_HIGH = 0;
 TIME_31a_LOW = 0;

 LearnCodeEvent = 0;
 LearnMode = 5 ;
 Cnt = 0;
#line 961 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 TRISC = 0b00000000;
 PORTC = 0x00;
#line 970 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 for( RFReceiverBufferCounter = 0; RFReceiverBufferCounter < 12 ;RFReceiverBufferCounter ++){
 RFReceiverBuffer [RFReceiverBufferCounter] = 0x00;
 }
 RFReceiverBufferCounter = 0;
#line 979 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 EEPROM_Codes_Cnt = EEPROM_Read(0x00) ;
 if(EEPROM_Codes_Cnt == 0xFF){
 EEPROM_Codes_Cnt=0;

 State = RFRemoteDecode_Detect_Sync_Bit;
 }
 else{
 State = RFRemoteDecode_Detect_Sync_Bit;

 for(i = 0; i<= EEPROM_Codes_Cnt; i++){

 TempHTC[i] = EEPROM_Read(0x01 + ((EEPROM_Codes_Cnt -1 )<<3));
 TempHTC[i] = TempHTC[i] + (EEPROM_Read(0x02 + ((EEPROM_Codes_Cnt-1)<<3))<<8);

 }

 }
#line 1043 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 TRISB =0b00000101;
 OPTION_REG =0b01000000 ;


 INTE_bit = 0;
 INTF_bit = 0;
#line 1095 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 PS2_bit = 1;
 PS1_bit = 1;
 PS0_bit = 1;
 TMR0 = 0x00;
 TMR0IE_bit = 0;
 TMR0IF_bit = 0;
#line 1104 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 INTCON = 0b11000000;
 T1CON = 1;
 PIR1.TMR1IF = 0;
 PIE1.TMR1IE = 1;
 T1CKPS1_bit = 0;
 T1CkPS0_bit = 1;

 TMR1H =0x00;
 TMR1L =0x00;
#line 1118 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 RC0_bit = 1;
 TMR0IE_bit = 1;
 INTE_bit = 1;
 INTEDG_bit = 1;

 while(1){

 switch ( State){
 case (RFRemoteDecode_Detect_Sync_Bit) :
 if( SyncBitCouter1 && ((EEPROM_Codes_Cnt != 0)||(LearnCodeEvent == 1) ) ){

 SyncBitCouter1 =0;

 RC0_bit =~ RC0_bit;



 if(LearnCodeEvent ==0 ){

 TempHTC1 = HighTickCounter + (HighTickCounter >>3);

 TempOneLow = TempHTC1 >> 2;
 TIME_1a_LOW = TempHTC1 - (TempOneLow);
 TIME_1a_HIGH = TempHTC1 + (TempOneLow);

 TIME_3a_LOW = (TIME_1a_LOW <<2) - TIME_1a_LOW;
 TIME_3a_HIGH = (TIME_1a_HIGH <<2) - TIME_1a_HIGH;


 }


 State =RFRemoteDecode_Detect_AddrnDat;
#line 1153 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 }
 if( Button( &PORTB, 2, 1,  0 )){


 RC5_bit = ~RC5_bit;
 INTE_bit = 0 ;
 TMR0 = 0;
 TMR0IE_bit = 1;
 Tmr0_Cnt = 0;
 State = RFRemoteDecode_Process_Button;
 }
 break;
 case (RFRemoteDecode_Detect_Min_High_Pulse) :

 if(DetectSyncBit ){

 if(EndOfTimingWindows){


 if((SyncBitCouter >= 3) && (abs(HTC[0] - HTC[1]) <=10)&& (abs(HTC[2] - HTC[1]) <=10) ){
 GIE_bit = 0;

 TMR0IE_bit = 0;

 if( LearnCodeEvent){
 RC1_bit = 1;


 TempHTC[EEPROM_Codes_Cnt] = HighTickCounter + (HighTickCounter >>3);

 TIME_1a_LOW = TempHTC[EEPROM_Codes_Cnt] - (TempHTC[EEPROM_Codes_Cnt] >> 2);
 TIME_1a_HIGH = TempHTC[EEPROM_Codes_Cnt] + (TempHTC[EEPROM_Codes_Cnt] >> 2);

 TIME_3a_LOW = (TIME_1a_LOW <<2) - TIME_1a_LOW;
 TIME_3a_HIGH = (TIME_1a_HIGH <<2) - TIME_1a_HIGH;

 TempCheck = TempHTC[EEPROM_Codes_Cnt] >> 8 ;

 TempOneLow = EEPROM_Codes_Cnt +(EEPROM_Codes_Cnt<<3);
 EEPROM_Write( (0x02 + ( TempOneLow)), ((unsigned short ) TempCheck));

 EEPROM_Write( (0x01 + ( TempOneLow)), ((unsigned short )(TempHTC[EEPROM_Codes_Cnt] )));

 State = RFRemoteDecode_Detect_Sync_Bit;

 }

 *(HTC) = 0;
 *(HTC+1) = 0;
 *(HTC+2) = 0;
 GIE_bit = 1;
 }
 SyncBitCouter = 0;

 }
 EndOfTimingWindows = 0;
 }
 DetectSyncBit = 0;

 break;
 case (RFRemoteDecode_Detect_AddrnDat) :

 EndOfTimingWindows = 0;
 SyncBitCouter = 0;
 if(CntFrame){


 INTE_bit = 0;
 PIE1.TMR1IE = 0;
#line 1226 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 Delay_ms(2);

 RFRemoteAddrCode= RFRemoteDecode_GetAddr(RFReceiverBuffer);
 RFRemoteDatCode = RFRemoteDecode_GetData(RFReceiverBuffer);
 for( RFReceiverBufferCounter = 0; RFReceiverBufferCounter < 12 ;RFReceiverBufferCounter ++){
 RFReceiverBuffer [RFReceiverBufferCounter] = 0x00;
 }
 RFReceiverBufferCounter = 0;

 CntFrame = 0;
 RC2_bit = ~RC2_bit;

 for(i = 1; i <= EEPROM_Codes_Cnt; i++){
 TempOneLow = i-1+((i-1)<<3);
 if((EEPROM_Read (0x03 + TempOneLow)==(unsigned short )(RFRemoteAddrCode )) &&
 (EEPROM_Read (0x04 + TempOneLow)==(unsigned short )(RFRemoteAddrCode >> 8 )) &&
 (EEPROM_Read (0x05 + TempOneLow)==(unsigned short )(RFRemoteAddrCode >> 16 )) &&
 (EEPROM_Read (0x06 + TempOneLow)==(unsigned short )(RFRemoteAddrCode >> 24 )) &&
 (EEPROM_Read (0x07 + TempOneLow)==(unsigned short )(RFRemoteDatCode )) &&
 (EEPROM_Read (0x08 + TempOneLow)==(unsigned short )(RFRemoteDatCode >> 8 )) ){


 TempOneLow = EEPROM_Read (0x09 + TempOneLow) ;
 if( TempOneLow == AllON){
 RC5_bit = 1;
 }
 else if( TempOneLow == AllOFF){
 RC5_bit = 0;
 }
 else{
 k = 1;
 if(j==0){
 RC5_bit=~RC5_bit;
 Tmr0_Cnt = 0;
 TMR0 = 0;
 j=1;
 }

 else{
 Tmr0_Cnt = 0;
 TMR0 = 0;
 }
 }
#line 1281 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/SmartHome-Centreal Controller/Source-SVN/SmartDigitalClock.c"
 break;
 }

 }
 if((k) ){

 INTE_bit = 1;
 PIE1.TMR1IE = 1;
 State = RFRemoteDecode_Detect_Sync_Bit;
 k=0;
 LearnCodeEvent = 0;
 }
 else{
 if(LearnCodeEvent){
 State =RFRemoteDecode_Establish_Code ;
 LearnCodeEvent = 0;
 }
 else{
 INTE_bit = 1;
 PIE1.TMR1IE = 1;
 State = RFRemoteDecode_Detect_Sync_Bit;

 }
 }
 }
 break;
 case (RFRemoteDecode_Process_Button) :
 while(1){

 if (Button( &PORTB, 2, 1,  1  )){
 Rc4_bit = ~RC4_bit;
 RC5_bit = ~ RC5_bit ;



 if((Tmr0_Cnt >=  61 ) &&(Tmr0_Cnt <  244 ) ){




 LearnCodeEvent = 1;

 INTE_bit = 1 ;
 TMR1H =0x00;
 TMR1L =0x00;
 State = RFRemoteDecode_Detect_Min_High_Pulse ;
 TMR0 = 0;
 Tmr0_Cnt1 = 0;

 }
 else if( Tmr0_cnt >=  244 ){

 EEPROM_Codes_Cnt = 0;
 EEPROM_Write(0x00, 0);
 State = RFRemoteDecode_Detect_Sync_Bit;
 }
 else{
 State = RFRemoteDecode_Detect_Sync_Bit;
 }

 INTE_bit = 1 ;
 TMR1H =0x00;
 TMR1L =0x00;
 Tmr0_Cnt = 0;
 break;
 }
 }
 EndOfTimingWindows = 0;
 SyncBitCouter = 0;

 break;
 case (RFRemoteDecode_Establish_Code) :
 EndOfTimingWindows = 0;
 SyncBitCouter = 0;


 TempOneLow = EEPROM_Codes_Cnt+ (EEPROM_Codes_Cnt<<3) ;
 EEPROM_Write( (0x03 + ( TempOneLow)), ((unsigned short )(RFRemoteAddrCode )));
 EEPROM_Write( (0x04 + ( TempOneLow)), ((unsigned short )(RFRemoteAddrCode >> 8 )));
 EEPROM_Write( (0x05 + ( TempOneLow)), ((unsigned short )(RFRemoteAddrCode >> 16 )));
 EEPROM_Write( (0x06 + ( TempOneLow)), ((unsigned short )(RFRemoteAddrCode >> 24 )));

 EEPROM_Write( (0x07 + ( TempOneLow)), ((unsigned short )(RFRemoteDatCode )));
 EEPROM_Write( (0x08 + ( TempOneLow)), ((unsigned short )(RFRemoteDatCode >> 8 )));
 EEPROM_Write( (0x09 + ( TempOneLow)), ((unsigned short )(LearnMode )));


 LearnMode = 5;
 EEPROM_Codes_Cnt =EEPROM_Codes_Cnt + 1 ;
 EEPROM_Write(0x00, EEPROM_Codes_Cnt);




 State = RFRemoteDecode_Detect_Sync_Bit;

 TMR0 =0;
 TMR1H =0x00;
 TMR1L =0x00;
 TMR0IE_bit = 1;
 INTEDG_bit = 0;
 Tmr1_Cnt = 0;




 INTE_bit = 1;
 PIE1.TMR1IE = 1;
 break;

 case (RFRemoteDecode_Control_Actuators ):
 break;
 }


 }
 }
